package com.lonely.fx.combo_box;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @date: 2022-07-18 12:14
 * @author: ztkj-hzb
 * @description:
 **/
public class 动态模糊匹配效果演示 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        ComboBox<User> comboBox = new ComboBox<>();
        comboBox.setPrefWidth(200);

        User user1 = new User().setName("小A").setAge(10);
        User user2 = new User().setName("小B").setAge(20);
        User user3 = new User().setName("小C").setAge(30);
        User user4 = new User().setName("小D").setAge(40);
        User user5 = new User().setName("小E").setAge(50);
        comboBox.getItems().addAll(user1, user2, user3, user4, user5);

        //保存数据源
        ObservableList<User> sourceItems = comboBox.getItems();

        comboBox.setEditable(true);
        comboBox.setPlaceholder(new Label("没有匹配项"));

        comboBox.setConverter(new StringConverter<User>() {
            @Override
            public String toString(User object) {
                if (object == null) {
                    return null;
                }
                return String.format("%s-%s", object.getName(), object.getAge());
            }

            @Override
            public User fromString(String string) {
                return null;
            }
        });

        //文本切换事件
        comboBox.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
            //过滤值
            FilteredList<User> filtered = sourceItems.filtered(user -> user.name.contains(newValue));
            comboBox.setItems(filtered);

            //这里，一定要先hide，再show，否则可视区里每次只能看见一行
            comboBox.hide();
            comboBox.show();
        });


        hBox.getChildren().addAll(comboBox);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }


    /**
     * 用户
     */
    private class User {

        private String name;

        private Integer age;

        public String getName() {
            return name;
        }

        public User setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public User setAge(Integer age) {
            this.age = age;
            return this;
        }
    }


}
